<template>
  <div>
    <el-card class="!border-none el-card-client relative" shadow="never">
      <temlate #header>
        <el-row :gutter="20">
          <el-col :span="18">
            <el-button
              @click="getLists"
              color="#63ccba"
              type="primary"
              class="btn_style mr_10"
            >
              <template #icon>
                <icon name="el-icon-Refresh" />
              </template>
              刷新列表
            </el-button>
          </el-col>
          <el-col :span="6" class="flex justify-end">
            <div class="flex justify-end">
              <el-button
                v-perms="['member.member_roles/add']"
                type="primary"
                @click="handleAdd"
                class="mr_10"
              >
                <template #icon>
                  <icon name="el-icon-Plus" />
                </template>
                新增账号
              </el-button>
              <!-- <move-button></move-button> -->
            </div>
          </el-col>
        </el-row>
      </temlate>
      <div>
        <el-table
          :data="pager.lists"
          border
          @selection-change="handleSelectionChange"
          v-loading="pager.loading"
          stripe
          max-height="calc(100vh - 11rem)"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column
            label="角色名称"
            prop="role_name"
            show-overflow-tooltip
          />
          <el-table-column
            label="角色的标识"
            prop="identify"
            show-overflow-tooltip
          />
          <el-table-column label="备注" prop="remark" show-overflow-tooltip />
          <el-table-column label="排序" prop="sort" show-overflow-tooltip />
          <el-table-column label="状态" prop="status">
            <template #default="{ row }">
              <dict-value :options="dictData.status" :value="row.status" />
            </template>
          </el-table-column>
          <el-table-column
            label="创建时间"
            prop="create_time"
            show-overflow-tooltip
          />

          <el-table-column label="操作" width="120" fixed="right">
            <template #default="{ row }">
              <el-button
                v-perms="['member.member_roles/edit']"
                type="primary"
                link
                @click="handleEdit(row)"
                >编辑</el-button
              >
              <el-button
                v-perms="['member.member_roles/delete']"
                type="danger"
                link
                @click="handleDelete(row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="flex position">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>
    <edit-popup
      v-if="showEdit"
      ref="editRef"
      :dict-data="dictData"
      @success="getLists"
      @close="showEdit = false"
    />
  </div>
</template>

<script lang="ts" setup name="memberRolesLists">
import { usePaging } from "@/hooks/usePaging";
import { useDictData } from "@/hooks/useDictOptions";
import { apiMemberRolesLists, apiMemberRolesDelete } from "@/api/member_roles";
import { timeFormat } from "@/utils/util";
import feedback from "@/utils/feedback";
import EditPopup from "./edit.vue";
import moveButton from "@/components/moveButton/index.vue";

const editRef = shallowRef<InstanceType<typeof EditPopup>>();
// 是否显示编辑框
const showEdit = ref(false);

// 查询条件
const queryParams = reactive({
  role_name: "",
  identify: "",
  remark: "",
  sort: "",
});

// 选中数据
const selectData = ref<any[]>([]);

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
  selectData.value = val.map(({ id }) => id);
};

// 获取字典数据
const { dictData } = useDictData("status");

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
  fetchFun: apiMemberRolesLists,
  params: queryParams,
});

// 添加
const handleAdd = async () => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("add");
};

// 编辑
const handleEdit = async (data: any) => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("edit");
  editRef.value?.setFormData(data);
};

// 删除
const handleDelete = async (id: number | any[]) => {
  await feedback.confirm("确定要删除？");
  await apiMemberRolesDelete({ id });
  getLists();
};

getLists();
</script>
<style scoped>
.btn_style {
  color: #fff;
}
</style>
